package com.tanhua.mqlistener;

import com.tanhua.api.mongdb.PublishApi;
import com.tanhua.domain.mogo.Publish;
import com.tanhua.template.HuaWeiUGCTemplate;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RocketMQMessageListener(
    topic = "tanhua-quanzi",
    consumerGroup="tanhua-quanzi-a"
)
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {
        // 1 获取id查询到该publish对象
        Publish publish = publishApi.findById(publishId);

        if(publish.getState()==0){
            //2 校验publish.文本
            boolean textFlag = huaWeiUGCTemplate.textContentCheck(publish.getTextContent());
            //3 校验publish.图片
            List<String> list = publish.getMedias();
            boolean imgFlag = huaWeiUGCTemplate.imageContentCheck(list.toArray(new String[]{}));

            Integer state=2; //未通过
            if(textFlag && imgFlag){
                state=1; //通过
            }

            //4 根据id修改publish对象的状态
            publishApi.updateState(publishId,state);

            System.out.println("已审核完成....");
        }




    }
}
